Page({
  data: {
    consultData: [],
    pageNo: 1,
    pageSize: 10,
    userId: 3,
    total: 0,
    hasMore: true
  },

  onLoad(options) {
    // 延迟加载数据，以确保页面已经准备好
    setTimeout(() => {
      this.loadData();
    }, 500);
  },

  loadData() {
    wx.request({
      url: `http://localhost:8080/api/my/answer/getAnswers`,
      data: {
        userId: this.data.userId,
        pageNo: this.data.pageNo,
        pageSize: this.data.pageSize
      },
      // 在loadData函数的success回调中,修改数据处理逻辑
      success: (res) => {
        if (res.data.code === 200) {
          const newData = res.data.data.list.map((item, index) => ({
            ...item,
            userName: item.userName || `用户${9527 + index}` // 如果userName为空,使用默认用户名
          }));
          const total = res.data.data.total;
          this.setData({
            consultData: [...this.data.consultData, ...newData],
            total: total,
            hasMore: this.data.consultData.length + newData.length < total
          });
        } else {
          wx.showToast({
            title: '加载失败',
            icon: 'none'
          });
        }
      },
      fail: (error) => {
        console.error("请求失败:", error);
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  onReachBottom() {
    if (this.data.hasMore) {
      this.setData({
        pageNo: this.data.pageNo + 1
      }, () => {
        this.loadData();
      });
    }
  }
});